package com.zygh.policeasset.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.system.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.policeasset.mapper.QrCodeAluminumMapper;
import com.zygh.policeasset.mapper.QrCodeGeneratedMapper;
import com.zygh.policeasset.mapper.QrCodeInfoMapper;
import com.zygh.policeasset.mapper.QrCodePublicAddressMapper;
import com.zygh.policeasset.pojo.QrCodeAluminum;
import com.zygh.policeasset.pojo.QrCodeInfo;
import com.zygh.policeasset.pojo.vo.QrCodeAluminumVO;
import com.zygh.policeasset.utils.EasyExcelUtil;
import com.zygh.policeasset.utils.UuidUtil;
import com.zygh.policeasset.utils.ZipUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/service/QrCodeAluminumService.class */
public class QrCodeAluminumService {
    @Value("${file.qrCode}")
    String qrCode;
    @Autowired
    QrCodeAluminumMapper qrCodeAluminumMapper;
    @Autowired
    EasyExcelUtil easyExcelUtil;
    @Autowired
    QrCodeGeneratedMapper qrCodeGeneratedMapper;
    @Autowired
    QrCodeInfoMapper qrCodeInfoMapper;
    @Autowired
    QrCodePublicAddressMapper qrCodePublicAddressMapper;

    @Transactional(rollbackFor = {Exception.class})
    public Integer add(QrCodeAluminum qrCodeAluminum) {
        try {
            qrCodeAluminum.setCreateTime(DateUtil.now());
            int count = this.qrCodeAluminumMapper.add(qrCodeAluminum).intValue();
            QrCodeInfo qrCodeInfo = new QrCodeInfo();
            for (int i = 0; i < qrCodeAluminum.getCounter().intValue(); i++) {
                qrCodeInfo.setQrCode(UuidUtil.get12UUID());
                qrCodeInfo.setQrCodeAluminumId(qrCodeAluminum.getId());
                qrCodeInfo.setType(1);
                this.qrCodeInfoMapper.addQrCodeInfo(qrCodeInfo);
            }
            return Integer.valueOf(count);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }
    }

    @Transactional(rollbackFor = {Exception.class})
    public Integer delete(Integer id) {
        try {
            this.qrCodeInfoMapper.deleteByQrCodeAluminumId(id);
            return this.qrCodeAluminumMapper.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }
    }

    public PageInfo<QrCodeAluminumVO> selectList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(this.qrCodeAluminumMapper.selectList());
    }

    public String selectQrCode(Integer qrCodeAluminumId, HttpServletResponse response) {
        List<QrCodeInfo> list = this.qrCodeInfoMapper.selectListByQrCodeAluminumId(qrCodeAluminumId);
        if (list.size() > 0) {
            return this.easyExcelUtil.exportAluminumQrCode(response, list);
        }
        return "";
    }

    public String exportQrCodeImage(Integer qrCodeAluminumId) throws FileNotFoundException {
        List<QrCodeInfo> list = this.qrCodeInfoMapper.selectListByQrCodeAluminumId(qrCodeAluminumId);
        String str = DateUtil.now().replaceAll("[[\\s-:punct:]]", "");
        File outFile = new File(System.getProperty(SystemUtil.USER_DIR) + this.qrCode + str);
        System.out.println(this.qrCode + str);
        if (!outFile.exists()) {
            outFile.mkdirs();
        }
        if (list.size() <= 0) {
            return "";
        }
        int length = list.size();
        for (int i = 0; i < length; i++) {
            QrCodeUtil.generate(this.qrCodePublicAddressMapper.queryPublicAddress() + list.get(i).getQrCode(), 500, 500, FileUtil.file(System.getProperty(SystemUtil.USER_DIR) + this.qrCode + str + "/" + (i + 1) + "_" + list.get(i).getQrCode() + ".jpg"));
        }
        ZipUtils.toZip(System.getProperty(SystemUtil.USER_DIR) + this.qrCode + str, new FileOutputStream(new File(System.getProperty(SystemUtil.USER_DIR) + this.qrCode + str + ".zip")), true);
        return this.qrCode + str + ".zip";
    }

    public PageInfo<QrCodeInfo> selectListByQrCodeAluminumId(int pageNum, int pageSize, Integer qrCodeAluminumId) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(this.qrCodeInfoMapper.selectListByQrCodeAluminumId(qrCodeAluminumId));
    }
}